package cn.xjfcloud.onedriveserver.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.xjfcloud.onedriveserver.pojo.dto.ApiConfig;
import cn.xjfcloud.onedriveserver.service.ApiConfigService;
import cn.xjfcloud.onedriveserver.util.ApiUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api
@RestController
public class ApiConfigController {

	@Autowired
	private ApiConfigService apiConfigService;
	
	@ApiOperation(value="获取Api Config", notes="获取Api Config")
	@GetMapping("getApiConfig/{id}")
	public Map<String, Object> getApiConfig(@ApiParam@PathVariable("id") Integer id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(id == null) {
			resultMap.put("code", "10001");
			resultMap.put("msg", "param is null");
			return resultMap;
		}else {
			ApiConfig apiConfig = apiConfigService.getApiConfigById(id);
			resultMap.put("code", "0");
			resultMap.put("apiConfig", apiConfig);
			return resultMap;
		}
	}
	
	
	/**
	 * 设置code 
	 * @param code
	 * @return
	 */
	@ApiOperation(value="设置code ", notes="设置code ")
	@GetMapping("code/{id}/{code}")
	public Map<String, Object> setCode(@ApiParam@PathVariable("id") Integer id, @ApiParam@PathVariable("code") String code) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//code 长度大于2 说明从新设置了code
		if(code.length() > 2) {
			Boolean getTokenByCodeFlag = apiConfigService.getTokenByCode(code,id);
			if (getTokenByCodeFlag == true) {
				resultMap.put("code", "0");
				resultMap.put("msg", "by code,success");
				return resultMap;
			} else {
				resultMap.put("code", "10001");
				resultMap.put("msg", "code error");
				return resultMap;
			}
		}else {
			Integer checkTokenFlag = apiConfigService.checkTokenStatus(id);
			//判断code的状态
			switch (checkTokenFlag) {
			//利用refresh_token获取
			case 1:
				Boolean getTokenByRefreshTokenFlag = apiConfigService.getTokenByRefreshToken(id);
				 if(getTokenByRefreshTokenFlag == false) {
					 	resultMap.put("code", "10002");
						resultMap.put("msg", "get token by refreshtoken fail!");
						return resultMap;
				 }else {
					 resultMap.put("code", "0");
					 resultMap.put("msg", "by refresh token,success");
					 return resultMap;
				 }
			//code还能使用
			case 2:
				resultMap.put("code", "0");
				resultMap.put("msg", "success");
				return resultMap;
			default:
				resultMap.put("code", "10003");
				resultMap.put("msg", "unknow");
				return resultMap;
			}
		}
		
		
	}
	
	/**
	 * 获取code状态
	 * @return
	 */
	@ApiOperation(value="获取code状态 ", notes="获取code状态 ")
	@GetMapping("getTokenStatus/{id}")
	public Map<String, Object> getTokenStatus(@ApiParam@PathVariable("id") Integer id){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code", "0");
		Integer checkTokenFlag = apiConfigService.checkTokenStatus(id);
		switch (checkTokenFlag) {
		case 0:
			resultMap.put("status", "null");
			break;
		case 1:
			resultMap.put("status", "out of date");
			break;
		case 2:
			resultMap.put("status", "available");
			break;
		default:
			resultMap.put("status", "unknow");
			break;
		}
		return  resultMap;
	}
	
	@ApiOperation(value="跳转获取code的url ", notes="设置code ")
	@GetMapping("getAuthCodeUrl/{id}")
	public void getAuthCodeUrl(@ApiParam()@PathVariable("id") Integer id,HttpServletRequest request, HttpServletResponse response){
		ApiConfig apiConfig = apiConfigService.getApiConfigById(id);
		String authCodeUrl = ApiUtil.getAuthCodeUrl(apiConfig);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		response.setStatus(301);
		response.setHeader("Location", authCodeUrl);
	}
}
